package us.westudio.manage.database;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DatabaseHandler {

	private SQLiteDatabase database;
	private DatabaseHelper databaseHelper;
	private String tableName;
	public static final String SELECT_ALL="*";
	
	public DatabaseHandler(Context context){
		this.database = null;
		this.databaseHelper = new DatabaseHelper(context);
		this.tableName = "";
	}
	
	public DatabaseHandler(Context context, String tableName){
		this.database = null;
		this.databaseHelper = new DatabaseHelper(context);
		this.tableName = tableName;
	}
	//set table name
	public void setTableName(String tableName){
		this.tableName = tableName;
	}
	
	//get writable database
	public void setWritableDatabase(){
		this.database = this.databaseHelper.getWritableDatabase();
	}
	
	//get readable database
	public void setReadableDatabase(){
		this.database = this.databaseHelper.getReadableDatabase();
	}
	
	//select databases
	public ArrayList<HashMap<String, String>>select(String selectInfo){
		return this.select(selectInfo, "");
	}
	
	public ArrayList<HashMap<String, String>>select(String tableName, String selectInfo, String condition){
		this.tableName = tableName;
		return this.select(selectInfo, condition);
	}
	public ArrayList<HashMap<String, String>>select(String selectInfo, String condition){
		ArrayList<String> tempSelectInfo = new ArrayList<String>();
		tempSelectInfo.add(selectInfo);
		return this.select(tempSelectInfo, condition);
	}
	
	public ArrayList<HashMap<String, String>>select(String tableName, String[] selectInfo, String condition){
		this.tableName = tableName;
		return this.select(selectInfo, condition);
	}
	public ArrayList<HashMap<String, String>> select(String[] selectInfo, String condition){
		ArrayList<String> tempSelectInfo = new ArrayList<String>();
		for(String value : selectInfo){
			tempSelectInfo.add( value );
		}
		return this.select(tempSelectInfo, condition);
	}
	public ArrayList<HashMap<String, String>> select(ArrayList<String> selectInfo, String condition){
		return this.select(this.tableName, selectInfo, condition);
	}
	public ArrayList<HashMap<String, String>> select(String tableName, ArrayList<String> selectInfo, String condition){
		StringBuffer query = new StringBuffer("SELECT ");
		Iterator<String> selecteInfoIterator = selectInfo.iterator();
		ArrayList<HashMap<String, String>>result = new ArrayList<HashMap<String,String>>();
		Cursor cursor;
		
		while(selecteInfoIterator.hasNext()){
			query.append( selecteInfoIterator.next() +",");
		}
		query.deleteCharAt(query.length() - 1);
		if(condition.length() != 0)
			query.append(" FROM "+this.formatTableName(tableName)+" WHERE "+condition);
		
		try {
			cursor = this.database.rawQuery(query.toString(), null);
			if(cursor.getCount() > 0){
				//cursor.moveToFirst(); //move to first cursor
				while(cursor.moveToNext()){  //not last cursor
					result.add( this.setColumnValues(cursor) );
				}
			}
			cursor.close();
		} catch (Exception e) {
		}
		return result;
	}
	
	//set column value
	private HashMap<String, String> setColumnValues(Cursor cursor){
		HashMap<String, String> resultPiece = new HashMap<String, String>();
		String columnValue = "",  columnKey="";
		for(int i = 0, length = cursor.getColumnCount(); i < length; i++){
			columnKey = cursor.getColumnName(i);
			try {
				columnValue = cursor.getString( cursor.getColumnIndexOrThrow( columnKey ) );
			} catch (Exception e1) {
				try {
					columnValue = String.valueOf( cursor.getInt(  cursor.getColumnIndexOrThrow(columnKey) )  );
				} catch (Exception e) {
					try {
						columnValue = String.valueOf( cursor.getBlob( cursor.getColumnIndexOrThrow( columnKey ) ) );
					} catch (Exception e2) {
						// TODO: handle exception
					}
				}
			}
			resultPiece.put(columnKey, columnValue);
		}
		return resultPiece;
	}
	
	//format table name
	private String formatTableName(String tableName){
		return tableName.toLowerCase();
	}
	
	
	//insert
	public boolean insert(String tableName, ContentValues insertValues){
		boolean status = true;
		try {
			if( this.database.insert(tableName, null, insertValues) == -1)
				status = !status;
		} catch (Exception e) {
			status = !status;
		}
		return status;
	}
	
	public boolean insert(ContentValues insertValues){
		return this.insert(DatabaseHelper.TABLE_NAME, insertValues);
	}
	
	//close database
	public void closeDatabase(){
		this.database.close();
	}
}
